<?php

namespace app\common\model;;

// use think\Model;

class TradeRecordModel extends BaseModel{

	protected $table = 'tp_trade_record';

	/*
	添加记录
	 */
	public function addData($buy_uid,$sell_uid,$order_id,$cid,$amount,$price,$fee){
		$data = [
			'buy_uid'=>$buy_uid,
			'sell_uid'=>$sell_uid,
			'order_id'=>$order_id,
			'order_no'=>date('Ymd-His',time()).$order_id,
			'cid' =>$cid,
			'amount' => $amount,
			'price'  => $price,
			'total_price'=>$amount * $price,
			'fee'=>$fee,
			'date' =>$this->getTime(),
			'status'=>1
		];
		$res = $this->insert($data);
		return $res;
	}

	/*
	进行中
	 */
	public function Tradeing($uid){
		$data = $this->alias('o')
						->join('tp_coin c','o.cid=c.id','LEFT')
						->where('o.sell_uid='.$uid.' or o.buy_uid='.$uid)
						->where('o.status','in','1,2')
						->order('o.id desc')
						->field('o.*,c.icon c_img,c.name c_name')
						->select();
		return $data;
	}
	
	/*
	已完成
	 */
	public function completeTrade($uid){
		$data = $this->alias('o')
						->join('tp_coin c','o.cid=c.id','LEFT')
						->where('o.sell_uid='.$uid.' or o.buy_uid='.$uid)
						->where('o.status','in','3,4,5')
						->order('o.id desc')
						->field('o.*,c.icon c_img,c.name c_name')
						->select();
		return $data;
	}

	/*
	已完成计数
	 */
	public function countComplete($uid){
		$data = $this->where('sell_uid='.$uid.' or buy_uid='.$uid)
						->where('status',3)
						->order('id desc')
						->count();
		return $data;
	}
	/*
	获取交易信息
	 */
	public function getTradeInfo($id){
		$data = $this->where('id',$id)->find();
		return $data;
	}
	/*
	标记付款
	 */
	public function confirmPay($id,$pay_cert,$remark){
		$data = [
			'pay_cert'=>$pay_cert,
			'remark'  =>$remark,
			'status'  =>2
		];
		$res  = $this->where('id',$id)->update($data);
		return $res;
	}
	/*
	确认订单
	 */
	public function confirmTrade($id){
		$res  = $this->where('id',$id)->update([
			'status'=>3
		]);
		return $res;
	}
	/*
	拒绝订单
	 */
	public function refuseTrade($id,$refuse_remark){
		$res = $this->where('id',$id)->update([
			'refuse_remark'=>$refuse_remark,
			'status' => 5   
		]);
		return $res;
	}
}